import Util from "../../utils/util";
import {getActivityDetail} from "../../api/activity";

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    parameter: {
    },
    opacity: 0,
    fields: {
      username: {title: "参与者姓名", required: true},
      age: {title: "年龄", required: true},
      mobile: {title: "手机号", required: true},
      address: {title: "所在地区", type: 'region', required: true},
      recommend_username: {title: "推荐机构/推荐人（如有）",},
    },

    batActive: true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;

    this.setData({
      bottom: 30,
      activity_id: options.id || 0,
      navH: app.globalData.navHeight
    })

    if (Util.isEmpty(this.data.activity_id)) {
      if (getCurrentPages().length > 1) {
        Util.Tips({title: "数据异常"}, {tab: 1})
      } else {
        Util.Tips({title: "数据异常"}, {tab: 4, url: "/pages/activity/index"})
      }
      return;
    }

    Util.chekWxLogin().then(res => {
      that.setData({
        userInfo: res.userInfo || null
      })

      wx.showLoading({title: '正在加载...'});
      getActivityDetail(that.data.activity_id).then(res => {
        that.setData({...res.data});
      }).catch(errmsg => {
        Util.Tips({title: errmsg}, {tab: 3})
      }).finally(() => {
        wx.hideLoading();
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  goBack(e) {
    let pages = getCurrentPages();
    if (pages.length > 1) {
      wx.navigateBack()
    } else {
      wx.switchTab({
        url: '/pages/activity/index'
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})